//
//  XDUnitCementSelectCourseHeaderView.m
//  XDEducation
//
//  Created by apple on 2017/4/15.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDUnitCementSelectCourseHeaderView.h"

@interface XDUnitCementSelectCourseHeaderView ()

/** title */
@property(nonatomic,strong) UILabel *titleLabel;

/** subtitle */
@property(nonatomic,strong) UILabel *subtitleLabel;

/** select button */
@property(nonatomic,strong) UIButton *selectButton;

/** line */
@property(nonatomic,strong) UIImageView *lineImageView;
@end

@implementation XDUnitCementSelectCourseHeaderView

-(instancetype)init{

    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = UIColorFromRGB(0x4c4c4c);
        [self addSubview:_titleLabel];
        
        
        _subtitleLabel = [[UILabel alloc]init];
        _subtitleLabel.font = [UIFont systemFontOfSize:16];
        _subtitleLabel.textColor = UIColorFromRGB(0x808080);
        [self addSubview:_subtitleLabel];
        
        _selectButton = [[UIButton alloc]init];
        _selectButton.userInteractionEnabled = NO;
        [_selectButton setImage:[UIImage imageNamed:@"list_btn_more_default"] forState:UIControlStateNormal];
        [_selectButton setImage:[UIImage imageNamed:@"list_btn_more_selected"] forState:UIControlStateSelected];
        [self addSubview:_selectButton];
        
        
        _lineImageView = [[UIImageView alloc]init];
        _lineImageView.image = [UIImage imageNamed:@"list_seg_list_default"];
        [self addSubview:_lineImageView];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerClicked:)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

-(void)layoutSubviews{

    [super layoutSubviews];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.centerY.equalTo(self.mas_centerY);
        //make.size.mas_equalTo(CGSizeMake(50, 20));
    }];
    
    [_subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLabel.mas_right).offset(15);
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    [_selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    [_lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-1);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(1);
    }];
}


-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}

-(void)setSubTitle:(NSString *)subTitle{
    _subTitle = subTitle;
    self.subtitleLabel.text = subTitle;
}


-(void)setIsOpen:(BOOL)isOpen{

    _isOpen = isOpen;
    _selectButton.selected = _isOpen;
}

-(void)headerClicked:(UITapGestureRecognizer *)tap{
    
    //tableView刷新了 button的状态重置了
    _selectButton.selected = !_selectButton.selected;
    
    if(self.headerViewClickedBlock){
        self.headerViewClickedBlock(_selectButton.selected);
    }
}
@end
